//
//  MyCollectViewController.m
//  shise
//
//  Created by 徐国庆 on 14/12/18.
//  Copyright (c) 2014年 MZP. All rights reserved.
//

#import "MyCollectViewController.h"
#import "DataBaseHaneler.h"
#import "MoreDetailCell.h"
#import "Food.h"
#import "UIImageView+WebCache.h"
#import "FoodDetailViewController.h"
#import "DetailViewController.h"
#import "UserDetailViewController.h"

@interface MyCollectViewController ()<UITableViewDataSource, UITableViewDelegate>

@property(nonatomic, retain)UITableView *tableView;
// 定义数组装收藏的美食数据
@property(nonatomic, retain)NSMutableArray *array;

@end

@implementation MyCollectViewController

// 初始化数组
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
        self.array = [NSMutableArray array];
        
    }
    return self;
}

- (void)dealloc
{
    [_tableView release];
    [_array release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor orangeColor];
    
    self.title = @"我的收藏";
    
    self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
    [_tableView release];
    
    [self handleData];
    
}

// 设置cell的个数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.array.count;
}

// 设置每一个cell里的数据
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    Food *food = [self.array objectAtIndex:indexPath.row];
    
    
    NSString *string = @"ss";
    MoreDetailCell *cell = [tableView dequeueReusableCellWithIdentifier:string];
    if (cell == nil) {
        
        cell = [[[MoreDetailCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:string]autorelease];
        
    }
    [cell.foodImageView setImageWithURL:[NSURL URLWithString:food.pic_url]];
//    [cell.userHeadImageView setImageWithURL:[NSURL URLWithString:[food.user objectForKey:@"avatar"]]];
//    [cell.userHeadImageView setImageWithURL:[NSURL URLWithString:food.userId]];
//    NSLog(@"人物头像%@" ,[NSURL URLWithString:[food.user objectForKey:@"avatar"]]);
    [cell.userHeadImageView setImageWithURL:[NSURL URLWithString:food.userHeadeImageViewAddress]];
    
    cell.userName.text = food.userName;
    cell.userAddress.text = food.location;
    cell.userSay.text = food.content;
    cell.userSay.font = [UIFont boldSystemFontOfSize:15];
    
    if (food.clickPraiseCount % 2 == 0) {
        
//        NSLog(@"点击赞的次数%ld", food.clickPraiseCount); 
        
        cell.praiseImageView.image = [UIImage imageNamed:@"praiseFont.png"];
    } else {
        
        cell.praiseImageView.image = [UIImage imageNamed:@"praiseBehind.png"];
    }
    
    cell.praise.text = @"赞";
    cell.praiseNumbers.text = food.likeCount;
    cell.markImageView.image = [UIImage imageNamed:@"markBehind.png"];
    cell.mark.text = @"收藏";
    // 设置tag值用来判断点击的图片是第几个cell上的
    // 美食图片tag值1000+
    cell.foodImageViewButton.tag = 1000 + indexPath.row;
    // 用户头像tag值2000+
    cell.userHeadImageViewButton.tag = 2000 + indexPath.row;
    // 赞图片tag值3000 +
    cell.praiseImageViewButton.tag = 3000 + indexPath.row;
    // 马克图片tag值4000 +
    cell.markImagViewButton.tag = 4000 + indexPath.row;
    // 设置点击美食图片按钮方法
    [cell.foodImageViewButton addTarget:self action:@selector(foodImageViewButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    // 设置点击头像图片按钮方法
    [cell.userHeadImageViewButton addTarget:self action:@selector(userHeadImagViewButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    // 设置点击赞图片按钮方法
    [cell.praiseImageViewButton addTarget:self action:@selector(praiseImageViewButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    // 设置点击马克图片按钮方法
    [cell.markImagViewButton addTarget:self action:@selector(markImagViewButtonAction:) forControlEvents:UIControlEventTouchUpInside];


//    cell.textLabel.text = @"cell";
    return cell;
}

// 定义方法从数据库中取出数据
- (void)handleData
{
    DataBaseHaneler *handler = [DataBaseHaneler shareInstance];
    // 打开数据库
    [handler openDB];
    // 查询数据
    
    NSUserDefaults *def = [NSUserDefaults standardUserDefaults];
    NSArray *allCollectArray = [handler selectAll];
    for (Food *tempFood in allCollectArray) {
        
        if ([tempFood.currentUserName isEqualToString:[def objectForKey:@"userName"]]) {
            
            [self.array addObject:tempFood];
        }
    }
    
//    self.array = [handler selectAll];
//    NSLog(@"已经收藏的美食数据%@", self.array);
}

// 设置cell的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    Food *food = [self.array objectAtIndex:indexPath.row];
    if (food.content.length == 0) {
        
        return 360;
    } else {
        
        return 390;
    }
}

// 点击美食图片上得按钮的时候执行这个方法
- (void)foodImageViewButtonAction:(UIButton *)button
{
//    NSLog(@"tag值%ld", button.tag);
    
    Food *food = [self.array objectAtIndex:button.tag - 1000];
    
    DetailViewController *detailVC = [[DetailViewController alloc] init];
    detailVC.detailApi = food.objId;
    [self.navigationController pushViewController:detailVC animated:YES];
    [detailVC release];
    
}

// 点击用户头像的时候执行这个方法
- (void)userHeadImagViewButtonAction:(UIButton *)button
{
//    
//    Food *food = [self.array objectAtIndex:button.tag - 2000];
//    NSDictionary *tempDic = food.user;
//    NSString *tempString = [tempDic objectForKey:@"id"];
//    UserDetailViewController *userDetailVC = [[UserDetailViewController alloc] init];
//    userDetailVC.userApi = tempString;
//    [self.navigationController pushViewController:userDetailVC animated:YES];
//    [userDetailVC release];

}

// 点击“赞”的时候执行的方法
- (void)praiseImageViewButtonAction:(UIButton *)button
{
    
    // 当点击赞的时候赞的图片变成红色的同时赞的人数加1
    NSInteger numberOfCell = button.tag - 3000;
    Food *food = [self.array objectAtIndex:numberOfCell];
    if (food.clickPraiseCount % 2 == 0) {
        
        food.clickPraiseCount += 1;
        
        NSString *likeCount = food.likeCount;
        NSInteger likeCountNew = [likeCount integerValue];
        likeCountNew = likeCountNew + 1;
        //    NSNumber *num = [likeCount description];
        food.likeCount = [NSString stringWithFormat:@"%ld", (long)likeCountNew];
        //        self.number = 1;
        [self.tableView reloadData];
        
    } else {
        
        food.clickPraiseCount += 1;
        
        NSString *likeCount = food.likeCount;
        NSInteger likeCountNew = [likeCount integerValue];
        likeCountNew = likeCountNew - 1;
        //    NSNumber *num = [likeCount description];
        food.likeCount = [NSString stringWithFormat:@"%ld", (long)likeCountNew];
//        self.number = 1;
        [self.tableView reloadData];
    }
}

// 点击“马克”的时候执行的方法
- (void)markImagViewButtonAction:(UIButton *)button
{
    NSInteger numberOfCell = button.tag - 4000;
    Food *food = [self.array objectAtIndex:numberOfCell];
    food.clickMarkCount += 1;
    [self.tableView reloadData];
    // 当收藏本条美食信息的时候就讲本条没事信息存储到本地 当点击我的收藏的时候从本地读取信息 并显示
    DataBaseHaneler *handler = [DataBaseHaneler shareInstance];
    // 打开数据库
    [handler openDB];
    // 创建美食表表
    [handler createFoodTable];
    // 添加一条美食数据
    [handler insertFood:food];
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
